<?php
class tb extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览帖子','1-2');
		$da = $this -> pub -> get_setting('帖子', 'TieBa');
		$this -> load -> view('adminx/tb', $da);
	}

	function get() {
		$this -> pub -> logp('获取帖子信息','1-2');
		if ($this -> input -> post('key')) {
			$this -> db -> select('count(*)');
			$this -> db -> like('nr', $this -> input -> post('key'));
			$q = $this -> db -> get_where('co_tb');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
			$this -> db -> like('name', $this -> input -> post('key'));
		} else {
			$this -> db -> select('count(*)');
			$q = $this -> db -> get_where('co_tb');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
		}
		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> order_by('id', 'desc');
		$q = $this -> db -> get_where('co_tb');
		$da['content'] = '';
		$state[1] = '<span class="label label-sm label-success">正常</span>';
		$state[2] = '<span class="label label-sm label-danger">禁用</span>';
		foreach ($q->result_array() as $v) {
			$this -> db -> select('headimgurl');
			$qq = $this -> db -> get_where('member', array('mid' => $v['mid']));
			$m = $qq -> row_array(0);

			$this -> db -> select('name');
			$qq = $this -> db -> get_where('co_main', array('id' => $v['cid']));
			$co = $qq -> row_array(0);

			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['id'] . '</td>';
			$da['content'] .= '<td><a target="_blank" href="/sgl/tx/' . $v['mid'] . '"><img width="48" src="' . ($m['headimgurl'] ? $m['headimgurl'] : '/mex/lance/img/tx.jpg') . '" /></a></td>';
			$da['content'] .= '<td>' . $co['name'] . '</td>';
			$da['content'] .= '<td>' . $v['nr'] . '</td>';
			$da['content'] .= '<td>' . $state[$v['state']] . '</td>';
			$da['content'] .= '<td>' . ($v['mktime'] ? date('Y-m-d', $v['mktime']) : '') . '</td>';
			$da['content'] .= '<td><button onclick="del(\'' . $v['id'] . '\')" type="button" class="btn default"><i class="fa fa-edit"></i>禁用</button>';
			$da['content'] .= '</td></tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

	function del() {
		$this -> pub -> logp('帖子删除 - '.$this -> input -> post('idx'),'1-2');
		$da['type'] = 1;
		$this -> db -> update('co_tb', array('state' => 2), array('id' => $this -> input -> post('idx')));
		echo json_encode($da);
	}

}
?>